<?php

namespace app\db\model;

use think\Db;
use think\Model;

class DbItemRelationData extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $dbName     = 'db_item_relation_data';

    # -------------------------------- 查询类接口 START ---------------------------------
    /**
     * 获取医务详情信息
     * @param int  $usercare_id 数据库ID
     * @author MSQ 2021-8-2
    **/
    public function getDetails($usercare_id = 0,$field = '*')
    {
        if ($field && $field != '*' && count(explode(',', $field)) == 1) {
            // 只取一个字段
            $info = $this->where(['id' => $usercare_id])->value($field);
        }else{
            $info = db($this->dbName)->field($field)->where(['id' => $usercare_id])->find();
        }
        
        return $info;
    }
    /**
     * 根据用户及目录获取itemRelationData
     * @param int  $user_id 用户id
     * @param int  $item_id 目录id
     * @author MSQ 2021-8-17
    **/
    public function getListByUserAndItemId($user_id = 0,$item_id = 0,$field = '*',$order= 'id ASC')
    {
        $where = [
            'status' => 1,
        ];
        $where['item_id'] = $item_id;
        if (is_array($item_id)) $where['item_id'] = ['IN',$item_id];
        
        $where['user_id'] = $user_id;
        if (is_array($user_id)) $where['user_id'] = ['IN',$user_id];

        $info = db($this->dbName)->field($field)->where($where)->order($order)->select();
        return $info;
    }
    # --------------------------------- 查询类接口 END -----------------------------------


    # ------------------------------ 数据处理类接口 START --------------------------------
    /**
     * 创建关联
     * @param int  $user_id 用户id
     * @param int  $item_id 模块id
     * @author MSQ 2021-8-2
    **/
    public function createItemRelationData($user_id = 0,$item_id = 0)
    {
        if (!$user_id || !$item_id) return false;
        # 是否是基础模块
        $basicItemId = model('DbItem')->getBasicItemInfo('id');
        if ($basicItemId == $item_id) {
            $where = [
                'user_id' => $user_id,
                'item_id' => $item_id,
                'status'  => 1,
            ];
            $isHasRelation = $this->where($where)->value('id');
            # 已经关联过基础模块
            if ($isHasRelation) return $isHasRelation;
        }
        $addData = [
            'user_id' => $user_id,
            'item_id' => $item_id,
        ];
        $this->setAttr('id',null)->isUpdate(false)->save($addData);
        if (!isset($this->id)) return false;
        return $this->id;
    }
    /**
     * 删除数据
     * @param int  $where 删除的用户id
     * @author MSQ 2021-8-2
    **/
    public function deleteItemRelationData($where = [])
    {
        if (!$where) return true;
        $where['status'] = 1;

        return $this->update(['status' => 0],$where);
    }
    /**
     * 模块关联回答排序
     * 适用于保存数据时
     * $sortItemRelationData item_id=>关联id(可多个)
     * @author MSQ 2021-8-18
     **/
    public function sortItemRelationData($sortItemRelationData){
        # 批量更新数据
        $saveAll = [];
        foreach ($sortItemRelationData as $k_item_id => $item_relations){
            $count = count($item_relations);
            foreach ($item_relations as $k=>$v){
                $saveAll[] = [
                    'id' => $v,
                    'weight' => $count,
                ];
                --$count;
            }
        }
        // $this->isUpdate()->saveAll($saveAll);
        $this->setAttr('id',null)->isUpdate(false)->saveAll($saveAll);

        return true;
    }
    # --------------------------------- 数据处理类接口 END ----------------------------------

    
}
